import { Color, DrawLayer_GUI, GameApp, IUserLogic, QUI_Canvas, QUI_Label, StateMgr, tt } from "./common.js";
import { GameConfig } from "./config/gameconfig.js";
import { State_Logo } from "./state/state_logo.js";

export class MainWin extends StateMgr implements IUserLogic {
    canvas: QUI_Canvas;

    fps_txt: QUI_Label;

    config:GameConfig;
    OnInit(): void {
        //初始化一个GUILayer
        //该项目是基于GUI的
        let guilayer = new DrawLayer_GUI();
        //这个设为1就会强行点对点，忽略windows 缩放
        tt.graphic.setMainScreenScale(1);

        let pr = tt.graphic.getDevicePixelRadio();//得到windows 缩放

        let scaleradio = (pr * 2);//按照windows 缩放的两倍来操作

        //取个整数
        let scaleint = scaleradio | 0;
        let left = scaleradio - scaleint;
        if (left > 0.6)
            scaleint++;
        console.log("ScaleRadio=" + scaleint);

        //改Camera的缩放会直接缩放内容
        guilayer.GetCamera().Scale = scaleradio;

        GameApp.GetViewList().AddDrawLayer(guilayer);


        //初始化canvas 和 fps
        this.canvas = guilayer.GetCanvas();

        this.fps_txt = new QUI_Label();
        this.fps_txt.localRect.setHPosByRightBorder(100);
        this.fps_txt.localRect.setVPosByTopBorder(20, 0);
        this.fps_txt.localColor = new Color(0.9, 0.8, 0.3, 1);
        this.canvas.AddChild(this.fps_txt);



        this.ChangeState(new State_Logo());
    }
    statemgr: StateMgr;
    timer: number = 0;
    framecount: number = 0;
    OnUpdate(delta: number): void {
        this.timer += delta;
        this.framecount++;
        if (this.timer > 1.0) {
            let fps = (this.framecount / this.timer * 100) | 0;
            this.framecount = 0;
            this.timer = 0;

            let fps_l = (fps % 100);
            let fps_h = (fps / 100) | 0;
            let fpsstr_h = fps_h.toString();
            while (fpsstr_h.length < 3)
                fpsstr_h = "0" + fpsstr_h;
            let fpsstr_l = fps_l.toString();
            while (fpsstr_l.length < 2)
                fpsstr_l += "0";
            let fpsstr = fpsstr_h + "." + fpsstr_l;
            this.fps_txt.text = "fps=" + fpsstr;
        }

        this.GetState()?.OnUpdate(delta);

    }
    OnExit(): void {
        this.GetState()?.OnExit();
    }
    OnResize(width: number, height: number): void {
        this.GetState()?.OnResize(width, height);
    }
    OnKey(keycode: string, press: boolean): void {
        this.GetState()?.OnKey(keycode, press);
    }
    OnPointAfterGUI(id: number, x: number, y: number, press: boolean, move: boolean): void {
        this.GetState()?.OnPointAfterGUI(id, x, y, press, move);
    }
    OnWheelAfterGUI(dx: number, dy: number, dz: number): void {
        this.GetState()?.OnWheelAfterGUI(dx, dy, dz);
    }

}